package com.sp.api.springbootapi.rabbitmq.direct;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * Created by weidewang on 2018/7/11
 */
public class Send {
    private final static String EXCHANGE_NAME="test_exchange_direct";
    public static void main(String[] args)  throws Exception{
        Connection connection = null;
        final Channel channel;

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setPort(5672);
        connection = factory.newConnection();
        //获取到连接以及通道
        channel = connection.createChannel();

        //声明exchange
        channel.exchangeDeclare(EXCHANGE_NAME,"direct");

        //消息内容
        String message = "删除商品，id = 10066";
        channel.basicPublish(EXCHANGE_NAME,"delete",null,message.getBytes()); //此处delete为路由键；
        System.out.println(" [x] Sent '"+ message+"'");
        channel.close();
        connection.close();
    }
}